package facilitator;

import java.util.List;

import core.CalculationException;
import core.DmuList;
import core.Optimization;
import core.Optimizer;
import core.PdmOptimization;

public class SolutionProvider {

	private int[] optimizationDirections;
	private List<PdmOptimization> transitionList;
	
	public List<PdmOptimization> getTransitionList() {
		return transitionList;
	}
	public int[] getOptimizationDirections() {
		return optimizationDirections;
	}
	public void setOptimizationDirections(int[] optimizationDirections) {
		this.optimizationDirections = optimizationDirections;
	}
	public void setOptimizationDirections(Optimization deaOpt, double[] absUrmWeights) {
		try {
			setOptimizationDirections(PdmOptimization.getOptimizationDirections(deaOpt, this.getRelWeights(absUrmWeights)));
		} catch (CalculationException e) {
			e.printStackTrace();
		}
	}
	private DmuList dmuList;
	
	public static double[] getRelWeights(double[] absWeights) {
		double[] relWeights = absWeights.clone();
		for(int i = 0;i<absWeights.length;i++){
			relWeights[i] = absWeights[i]/absWeights[0];
		}
		return relWeights;
	}
	public Optimization getDeaOptimization(){
		try {
		Optimization deaOpt;
		
			deaOpt = new Optimization(this.dmuList);
			Optimizer.calculateDea(deaOpt);
			return deaOpt;
		} catch (CalculationException e) {
			e.printStackTrace();
		}
		return null;
	}
	public PdmOptimization getPdmOptimization(double alpha, double[] absUrmWeights){
		try{
			PdmOptimization pdmOpt = new PdmOptimization(this.dmuList, alpha, absUrmWeights, this.optimizationDirections);
			Optimizer.calculatePdm(pdmOpt);
			return pdmOpt;
		} catch (CalculationException e) {
			e.printStackTrace();
		}
		return null;
	}
	public void calcTransitionList(double[] absUrmWeights){
		try {
			TransitionExplorer expl = new TransitionExplorer(this.dmuList, absUrmWeights, this.optimizationDirections);
			this.transitionList = expl.getTransitionList();
		} catch (CalculationException e) {
			e.printStackTrace();
		}
	}
	public static double[] getRelOutputWeights(Optimization opt) throws CalculationException{
		double[] out = new double[opt.getOptimizationWeights().length-opt.getDmuList().inputSize];
		for(int i = 0;i<out.length;i++){
			out[i] = opt.getOptimizationWeights()[i]/opt.getOptimizationWeights()[0];
		}
		return out;
	}
	public SolutionProvider(DmuList dmuList){
		super();
		this.dmuList = dmuList;
	}
	public SolutionProvider(DmuList dmuList, int[] optimizationDirections) {
		this(dmuList);
		this.optimizationDirections = optimizationDirections;
	}
}
